﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebUI.DAL;
using System.Data.Linq;
namespace WebUI.BL.User
{
    /// <summary>
    /// Quan ly User, thao tac nghiep vu.
    /// </summary>
    public class UserBL
    {
        /* template
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
                {
                    try
                    {

                    }
                    catch (ChangeConflictException)
                    {
                        //neu xay ra loi rollback lai
                        contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                        contextdb.SubmitChanges();
                    }
                }
        
         */

        public void Insert(tbl_User user)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_User newUser = new tbl_User
                    {
                        UserNo = user.UserNo,
                        UserLoginName = user.UserLoginName,
                        Status = user.Status,
                        Phone = user.Phone,
                        Password = user.Password,
                        Name = user.Name,
                        IdentityCardId = user.IdentityCardId,
                        Email = user.Email,
                        Department = user.Department,
                        Addess = user.Addess
                    };
                    contextdb.tbl_Users.InsertOnSubmit(newUser);
                    contextdb.SubmitChanges();
                }
                catch(ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }
        public void Update(tbl_User user)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var updateUser = contextdb.tbl_Users.SingleOrDefault(u => u.UserNo == user.UserNo);
                try
                {
                    if (updateUser != null)
                    {
                        updateUser.UserLoginName = user.UserLoginName;
                        updateUser.Status = user.Status;
                        updateUser.Phone = user.Phone;
                        updateUser.Password = user.Password;
                        updateUser.Name = user.Name;
                        updateUser.IdentityCardId = user.IdentityCardId;
                        updateUser.Email = user.Email;
                        updateUser.Department = user.Department;
                        updateUser.Addess = user.Addess;

                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
                
            }
        }
        public void Delete(int Id)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                var deleteUser = contextdb.tbl_Users.SingleOrDefault(u => u.UserNo == Id);
                try
                {
                    if (deleteUser != null)
                    {
                        contextdb.tbl_Users.DeleteOnSubmit(deleteUser);
                        contextdb.SubmitChanges();
                    }
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    contextdb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    contextdb.SubmitChanges();
                }
            }
        }

        public List<tbl_User> GetUsers()
        {
            AdlerContextDataContext dbcontext = new AdlerContextDataContext();
            return dbcontext.tbl_Users.ToList();
        }

        //public List<tbl_User> GetUsers(int page=1)
        //{
        //    return SingletonContext.GetInstance().tbl_Users.OrderBy(p => p.UserNo).Skip((page - 1) * PageSize).Take(PageSize);
        //}

        public tbl_User GetUserById(int Id)
        {
            AdlerContextDataContext dbcontext = new AdlerContextDataContext();
            return dbcontext.tbl_Users.FirstOrDefault(x => x.UserNo == Id);
        }

        public tbl_User GetUserByCode(int Code)
        {
            AdlerContextDataContext dbcontext = new AdlerContextDataContext();
            return dbcontext.tbl_Users.FirstOrDefault(x => x.UserNo == Code);
        }

        public tbl_User GetUserByStatus(string status)
        {
            AdlerContextDataContext dbcontext = new AdlerContextDataContext();
            return dbcontext.tbl_Users.SingleOrDefault(x => x.Status == status);
        }

        public List<tbl_User> GetUsersByName(string name)
        {
            AdlerContextDataContext dbcontext = new AdlerContextDataContext();
            return dbcontext.tbl_Users.Where(x => x.Name.StartsWith(name)).ToList();
        }


    }
}
